package com.example.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Service.CgOrderService;
import com.example.dao.CgOrderMapper;
import com.example.exception.MyException;
import com.example.pojo.CgOrder;
import com.example.util.Result;
import com.example.util.ThreadLocalUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/cgorder")
@Api(tags = "采购")
public class CgOrderController {
    @Resource
    private CgOrderMapper cgOrderMapper;
    @Resource
    private CgOrderService cgOrderService;

//  根据id查询采购请求信息
@GetMapping("/getList")
public Object getList(){
    System.out.println( ThreadLocalUtil.getCurrentId());
    QueryWrapper<CgOrder>
            queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("create_by", ThreadLocalUtil.getCurrentId());
    List<CgOrder> cgOrderList=cgOrderService.list(queryWrapper);

    return new Result<>("200","查询成功",cgOrderList);
 }
//

//  添加采购
 @PostMapping("/add")
    public Object add(@RequestBody CgOrder cgOrder){
    cgOrder.setTotal(cgOrder.getPrice()* cgOrder.getQuantity());
     System.out.println(cgOrder.getTotal());
    if (!cgOrderService.save(cgOrder)) {
        throw new MyException("459","添加采购信息失败");
    }
     return new Result<>("200","添加采购信息成功");
 }

//  撤销采购
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){

//                逻辑删除
        if (!cgOrderService.removeById(id)){
            throw new MyException("460","撤销采购信息失败");
        }
        return new Result<>("200","撤销采购信息成功");
    }


//   采购财务审批资格判断
    @GetMapping("/getFinanceList")
    public Object getFinanceList(
            @RequestParam(value = "PageNum",defaultValue = "1") int PageNum,
            @RequestParam(value = "PageSize",defaultValue = "5") int PageSize
    ){
        HashMap<String,Object> map=new HashMap<>();
        QueryWrapper<CgOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("review","已通过");
        queryWrapper.eq("allocation","请求中");
        Page<CgOrder> page = new Page<>(PageNum,PageSize);
        IPage<CgOrder> cgOrderIPage =cgOrderService.page(page, queryWrapper);
        map.put("total",cgOrderIPage.getTotal());
        map.put("rows",cgOrderIPage.getRecords());
        map.put("pages",cgOrderIPage.getPages());
        return new Result<>("200","查询成功",map);
    }


    //   采购财务审批资格判断
    @GetMapping("/getFinanceList1")
    public Object getFinanceList1(
            @RequestParam(value = "PageNum",defaultValue = "1") int PageNum,
            @RequestParam(value = "PageSize",defaultValue = "") int PageSize
    ){
        HashMap<String,Object> map=new HashMap<>();
        QueryWrapper<CgOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("review","已通过");
        queryWrapper.eq("allocation","已拒绝");
        queryWrapper.eq("allocation","已通过");
        Page<CgOrder> page = new Page<>(PageNum,PageSize);
        IPage<CgOrder> cgOrderIPage =cgOrderService.page(page, queryWrapper);
        map.put("total",cgOrderIPage.getTotal());
        map.put("rows",cgOrderIPage.getRecords());
        map.put("pages",cgOrderIPage.getPages());
        return new Result<>("200","查询成功",map);
    }
//    采购财务审批
    @PutMapping("/updateFinance")
    public Object updateFinance(
            @RequestParam("id") Integer id,
            @RequestParam("allocation") String allocation,
            @RequestParam("renSonb") String renSonb
    ){
        QueryWrapper<CgOrder>
                queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        CgOrder cgOrder = cgOrderService.getOne(queryWrapper);
        //判断是否存在
        if (cgOrder==null){
            return new Result<>("200","采购信息不存在",null);
        }
        cgOrder.setAllocation(allocation);
        cgOrder.setRenSonb(renSonb);
        if (!cgOrderService.updateById(cgOrder)){

            return new Result<>("200","采购信息更新失败",null);
        }
        return new Result<>("200","采购信息更新成功",null);
    }

////   采购审批
//    @PutMapping("/update")
//    public Object update(@RequestBody CgOrder cgOrder){
//        System.out.println(cgOrder.getId());
//        cgOrder.getAllocation();
//        if (!cgOrderService.updateById(cgOrder)){
//            throw new MyException("461","采购审批失败");
//        }
//        return new Result<>("200","采购审批成功");
//    }







}
